package com.smart.service.impl;

import java.util.List;
import java.util.Arrays;
import com.smart.util.Pageable;
import com.smart.entity.SysRole;
import javax.annotation.Resource;
import com.smart.mapper.RoleMapper;
import cn.hutool.core.util.StrUtil;
import com.smart.service.RoleService;
import com.smart.common.enums.StatusEnums;
import com.smart.common.util.SysUserUtils;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

/**
 * @author lgb
 * @date 2020年6月10日 下午7:38:28
 * Copyright (C) 深圳工勘科技
 * @说明: 智慧工地serviceImpl
 **/
@Service
public class RoleServiceImpl extends BaseServiceImpl<SysRole,
		Long> implements RoleService {

	@Resource
	private RoleMapper roleMapper;
	
	/**
	 * @param pageable
	 * @see 分页查询数据
	 **/
	@Override
	@Transactional(readOnly=true)
	public com.smart.util.Page<SysRole> findPage(Pageable pageable) {
		LambdaQueryWrapper<SysRole> wrapper=new LambdaQueryWrapper<>();
		return super.findPage(pageable,wrapper);
	}

	/**
	 * (non-Javadoc)
	 * @see 获取分页数据 
	 **/
	@Override
	@Transactional(readOnly=true)
	public IPage<SysRole> getPage(SysRole sysRole,Integer pageSize,
			Integer pageNum) {
		LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>(sysRole);

        if (StrUtil.isNotEmpty(sysRole.getBeginTime())) {
            wrapper.gt(SysRole::getCreateTime, sysRole.getBeginTime());
        }

        if (StrUtil.isNotEmpty(sysRole.getEndTime())) {
            wrapper.lt(SysRole::getCreateTime, sysRole.getEndTime());
        }
        return page(new Page<>(pageNum,pageSize),wrapper);
	}
	/** 
	 * (non-Javadoc)
	 * @see: 获取所有的角色列表
	 **/
	@Override
	@Transactional(readOnly=true)
	public List<SysRole> getListAll() {
		Long userId=SysUserUtils.getSysUser().getId();
		if(userId.intValue()==1L){
			return list(new LambdaQueryWrapper<SysRole>()
	                .eq(SysRole::getStatus,StatusEnums.ENABLE.getKey()));
		}
		//不查询高管
		return list(new LambdaQueryWrapper<SysRole>().notIn(SysRole::getId,2)
                .eq(SysRole::getStatus,StatusEnums.ENABLE.getKey()));
	}

	/** 
	 * (non-Javadoc)
	 * @see: 创建角色
	 **/
	@Override
	@Transactional(rollbackFor=RuntimeException.class)
	public int create(SysRole sysRole) {
		int re = this.baseMapper.insert(sysRole);
		roleMapper.insertRoleAndRoleMenu(sysRole.getId(),sysRole.getMenuIds());
        return re;
	}

	@Override
	public int deleteByIds(Long[] ids) {
        //删除角色与菜单关联 删除角色与用户关联
        roleMapper.deleteUsersByRoleIds(ids);
        roleMapper.deleteMenusByRoleIds(ids);
        return this.baseMapper.deleteBatchIds(Arrays.asList(ids));
	}

	@Override
	@Transactional(rollbackFor=RuntimeException.class)
	public int update(Long id, SysRole sysRole) {
		sysRole.setId(id);
		Long[] ids = {id};
		roleMapper.deleteMenusByRoleIds(ids);
        if (!CollectionUtil.isEmpty(sysRole.getMenuIds())) {
            //保存角色与菜单关系
        	roleMapper.insertRoleAndRoleMenu(sysRole.getId(),sysRole.getMenuIds());
        }
        return this.baseMapper.updateById(sysRole);
	}
	
}